﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VswService_V7.DataBase.Tables;
using VswService_V7.Utility;

namespace VswService_V7.ClientCommand
{
    public class ClientMessageHandler
    {
        //public BlockingCollection<CommandContent> messageList;     //IproductionCuonsumer
        public ConcurrentQueue<CommandContent> messageList;   // IproductionCuonsumer
        private static ClientMessageHandler _messageHandler;
        public static ClientMessageHandler MessageHandler
        {
            get
            {
                if (_messageHandler == null)
                {
                    _messageHandler = new ClientMessageHandler();
                }
                return _messageHandler;
            }
        }


        public bool IsHandling
        {
            private set;
            get;
        }

        public ClientMessageHandler()
        {
            messageList = new ConcurrentQueue<CommandContent>();
            IsHandling = true;
            Task.Run(() => HandleMessage());
        }

        private async Task HandleMessage()
        {
			try
			{
				while (IsHandling)
				{
					if (!messageList.IsEmpty)
					{
						CommandContent content = new CommandContent();
						bool s = messageList.TryDequeue(out content);
						if (s)
						{
							switch (content.commandId)
							{
								#region Login
								case Identity.login:
									{
										try
										{
											string userName = content.body[0];
											string password = content.body[1];
											DataTable[] t = USER_TABLE.Login(userName, password);
											if (t != null && t.Length == 3)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.login);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								#endregion

								#region UserManager
								//分页查找用户
								case Identity.selectUSER_BYINDEX:
									{
										try
										{
											int minRow = Convert.ToInt32(content.body[0]);
											int maxRow = Convert.ToInt32(content.body[1]);
											string totalPages = "";
											DataTable t = USER_TABLE.SelectUSER_BYINDEX(minRow, maxRow,out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t)+";"+ totalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.selectUSER_BYINDEX);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								//修改用户
								case Identity.update_USER:
									{
										try
										{
											string userid = content.body[0];
											string username = content.body[1];
											string userlevel = content.body[2];
											string department = content.body[3];
											string shift = content.body[4];
											DataTable t = USER_TABLE.Update_USER(userid, username, userlevel, department, shift);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.update_USER);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								//删除用户
								case Identity.delete_USER:
									{			
										try
										{
											string userNo = content.body[0];
											string totalPages = "";
											DataTable t = USER_TABLE.Delete_USER(userNo,out totalPages);
											if (t !=null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.delete_USER);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								//重置用户密码
								case Identity.reset_USERPASSWORD:
									{
										try
										{
											string userid = content.body[0];
											int t = USER_TABLE.Reset_USERPASSWORD(userid);
											if (t > 0)
											{
												string back = FormatTcpString(content.commandId, "1", "1");
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.reset_USERPASSWORD);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								//添加用户
								case Identity.insert_USER:
									{
										try
										{
											string userid = content.body[0];
											string username = content.body[1];
											string userlevel = content.body[2];
											string creatdate = content.body[3];
											string department = content.body[4];
											string shift = content.body[5];
											string password = content.body[6];
											string totalPages = "";
											DataTable t = USER_TABLE.Insert_USER(userid, username, userlevel, creatdate, department, shift, password,out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.insert_USER);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								//修改密码
								case Identity.update_USERPASSWORD:
									{
										try
										{
											string userid = content.body[0];
											string password = content.body[1];
											int t = USER_TABLE.Update_USERPASSWORD(userid, password);
											if (t > 0)
											{
												string back = FormatTcpString(content.commandId, "1", "1");
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.update_USERPASSWORD);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								//查询用户信息
								case Identity.select_USERINFO:
									{
										try
										{
											string userid = content.body[0];
											DataTable t = USER_TABLE.Select_USERINFO(userid);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_USERINFO);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								//编辑班组
								case Identity.update_SHIFT:
									{
										try
										{
											DataTable shiftTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											DataTable t = SHIFT_TABLE.ResetShiftTable(shiftTable);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.update_SHIFT);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								//编辑Team
								case Identity.update_TEAM:
									{
										try
										{
											DataTable teamTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											DataTable t = TEAM_TABLE.ResetTeamTable(teamTable);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.update_SHIFT);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.refrushShiftTable:
									{
										SHIFT_TABLE workTable = new SHIFT_TABLE();
										try
										{
											DataTable t = SHIFT_TABLE.GetShiftTable();
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.update_SHIFT);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								#endregion

								#region General Handle
								case Identity.initPageTask:
									{
										try
										{
											string generalTaskTotalPages = "";
											string taskTaskTotalPages = "";
											string recipeTotalPages = "";
											string batchSRTotalPages = "";
											string stripSRTotalPages = "";
											DataTable[] t = TASK_INFO.InitPageTask(out generalTaskTotalPages, out taskTaskTotalPages, out recipeTotalPages, out batchSRTotalPages, out stripSRTotalPages);
											if (t != null && t.Length == 6)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + generalTaskTotalPages + ";" + taskTaskTotalPages + ";" + recipeTotalPages + ";" + batchSRTotalPages + ";" + stripSRTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.initPageOrder);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}							
								case Identity.select_GeneralTaskByIndex:
									{
										try
										{
											int minRow = Convert.ToInt32(content.body[0]);
											int maxRow = Convert.ToInt32(content.body[1]);
											string generalTaskTotalPages = "";
											DataTable t = TASK_INFO.SelectGeneralTaskByIndex(minRow, maxRow, out generalTaskTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + generalTaskTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_RecipeByIndex);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								#endregion

								#region Task Handle 
								//Task 								
								case Identity.refreshTaskTask:
									{
										try
										{
											int minRow = Convert.ToInt32(content.body[0]);
											int maxRow = Convert.ToInt32(content.body[1]);
											string generalTaskTotalPages = "";
											DataTable t = TASK_INFO.SelectTaskTaskByIndex(minRow, maxRow, out generalTaskTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + generalTaskTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.refreshGeneralTask);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.selectTaskByParameters:
									{
										try
										{
											string sql = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string taskTotalPages = "";
											DataTable t = TASK_INFO.SelectTaskByParameters(sql, minRow, maxRow, out taskTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + taskTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "selectTaskByParameters Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("selectTaskByParameters", ex);
										}
										break;
									}
								case Identity.insert_TASK:
									{
										try
										{
											DataTable taskdataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											string userNo = content.body[1];
											DataTable t = TASK_INFO.Insert_TASK(taskdataTable, userNo);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.insert_ORDER);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.updateTaskByTaskNo:
									{
										try
										{
											DataTable taskDataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											string userNo = content.body[1];
											DataTable t = TASK_INFO.UpdateTaskByTaskNo(taskDataTable, userNo);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.update_ORDERByOrderId);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.deleteUnloadingTask:
									{
										try
										{
											string taskNo = content.body[0];
											string userNo = content.body[1];
											int minRow = Convert.ToInt32(content.body[2]);
											int maxRow = Convert.ToInt32(content.body[3]);
											string taskTotalPages = "";
											DataTable t = TASK_INFO.DeleteUnloadingTask(taskNo, userNo,minRow,maxRow, out taskTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + taskTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.deleteUnfinishedOrder);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								
								
								#endregion
								#region Strip Handle
								case Identity.insert_STRIP_INFO:
									{
										try
										{
											DataTable stripdataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											string userNo = content.body[1];
											string totalPages = "";
											DataTable t = STRIP_INFO.Insert_STRIP_INFO(stripdataTable, userNo, out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.insert_ORDER);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.updateStripByStripNo:
									{
										try
										{
											DataTable stripdataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											string userNo = content.body[1];
											DataTable t = STRIP_INFO.UpdateStripByStripNo(stripdataTable, userNo);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.insert_ORDER);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.deleteStripInfoByStrip_code:
									{
										try
										{
											string stripNo = content.body[0];
											string userNo = content.body[1];
											int minRow = Convert.ToInt32(content.body[2]);
											int maxRow = Convert.ToInt32(content.body[3]);
											string totalPages = "";
											DataTable t = STRIP_INFO.DeleteStripInfoByStrip_code(stripNo, userNo, minRow, maxRow, out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.deleteStripInfoByStrip_code);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.select_STRIP_INFOByTaskNo:
									{
										try
										{
											string task_No = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string totalPages = "";
											DataTable t = STRIP_INFO.Select_STRIP_INFOByTaskNo(task_No, minRow, maxRow, out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											LogHelper.WriteLog("select_STRIP_INFOByTaskNo", ex);
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								#endregion
								#region Recipe Handle
								case Identity.insert_Recipe:
									{
										try
										{
											DataTable recipedataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											DataTable t = RECIPE_INFO.Insert_Recipe(recipedataTable);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.insert_Recipe);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.edit_Recipe:
									{
										try
										{
											DataTable recipedataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											DataTable t = RECIPE_INFO.Edit_Recipe(recipedataTable);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.edit_Recipe);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.deleteRecipeByRecipe_id:
									{
										try
										{
											string recipeNo = content.body[0];
											string userName = content.body[1];
											string recipeTotalPages = "";
											DataTable t = RECIPE_INFO.DeleteRecipeByRecipe_id(recipeNo, userName, out recipeTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + recipeTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.deleteRecipeByRecipe_id);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.select_RecipeByIndex:
									{
										try
										{
											int minRow = Convert.ToInt32(content.body[0]);
											int maxRow = Convert.ToInt32(content.body[1]);
											string recipeTotalPages = "";
											DataTable t = RECIPE_INFO.Select_RecipeByIndex(minRow, maxRow, out recipeTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + recipeTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_RecipeByIndex);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.selectRecipeByParameters:									
									{
										try
										{
											string sql = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string TotalPages = "";
											DataTable t = RECIPE_INFO.SelectRecipeByParameters(sql, minRow, maxRow, out TotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + TotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "selectRecipeByParameters Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("selectRecipeByParameters", ex);
										}
										break;
									}

								#endregion
								#region BatchSampleRule Handle
								case Identity.insert_BatchSampleRule:
									{
										try
										{
											DataTable batchdataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											DataTable t = BATCH_SAMPLE_RULE.Insert_BatchSampleRule(batchdataTable);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "insert_BatchSampleRule Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.edit_BatchSampleRule:
									{
										try
										{
											DataTable batchdataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											DataTable t = BATCH_SAMPLE_RULE.Edit_BatchSampleRule(batchdataTable);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "edit_BatchSampleRule Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.delete_BatchSampleRule:
									{
										try
										{
											string sampleNo = content.body[0];
											DataTable t = BATCH_SAMPLE_RULE.Delete_BatchSampleRule(sampleNo);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.deleteRecipeByRecipe_id);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.select_BatchSampleRuleByIndex:
									{
										try
										{
											int minRow = Convert.ToInt32(content.body[0]);
											int maxRow = Convert.ToInt32(content.body[1]);
											string batchTotalPages = "";
											DataTable t = BATCH_SAMPLE_RULE.Select_BatchSampleRuleByIndex(minRow, maxRow, out batchTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + batchTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_RecipeByIndex);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								#endregion
								#region StripSampleRule Handle
								case Identity.insert_StripSampleRule:
									{
										try
										{
											DataTable stripdataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											DataTable t = STRIP_SAMPLE_RULE.Insert_StripSampleRule(stripdataTable);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.insert_Recipe);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.edit_StripSampleRule:
									{
										try
										{
											DataTable stripdataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[0]);
											DataTable t = STRIP_SAMPLE_RULE.Edit_StripSampleRule(stripdataTable);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.edit_Recipe);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.delete_StripSampleRule:
									{
										try
										{
											string sampleNo = content.body[0];
											DataTable t = STRIP_SAMPLE_RULE.Delete_StripSampleRule(sampleNo);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.deleteRecipeByRecipe_id);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.select_StripSampleRuleByIndex:
									{
										try
										{
											int minRow = Convert.ToInt32(content.body[0]);
											int maxRow = Convert.ToInt32(content.body[1]);
											string stripTotalPages = "";
											DataTable t = STRIP_SAMPLE_RULE.Select_StripSampleRuleByIndex(minRow, maxRow, out stripTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + stripTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_RecipeByIndex);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								#endregion
								#region ProductionStandard Handle
								case Identity.selectProductionStandard:
									{
										try
										{											
											DataTable t = PRODUCTION_STANDARD.SelectProductionStandard();
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "selectProductionStandard Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.insert_ProductionStandard:
									{
										try
										{
											string standardCode= content.body[0];
											string standardName = content.body[1];
											DataTable t = PRODUCTION_STANDARD.Insert_ProductionStandard(standardCode, standardName);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "insert_ProductionStandard Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.edit_ProductionStandard:
									{
										try
										{
											string standardCode = content.body[0];
											string standardName = content.body[1];
											DataTable t = PRODUCTION_STANDARD.Edit_ProductionStandard(standardCode, standardName);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "edit_ProductionStandard Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}								
								#endregion


								#region DownTime Handle			
								case Identity.innitPageDownTime:
									{
										try
										{
											string downtimeTotalPages = "";
											DataTable t = DOWNTIME_TABLE.InnitPageDownTime(out downtimeTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + downtimeTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.innitPageDownTime);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.innitPageDownTime", ex);
										}
										break;
									}
								case Identity.updatedowntime:
									{
										try
										{
											string downtimeno = content.body[0];
											string stoptype = content.body[1];
											string machineno = content.body[2];
											string starttime = content.body[3];
											string endtime = content.body[4];
											string serviceteam = content.body[5];
											string stopreson = content.body[6];
											string status = content.body[7];
											string downcommom = content.body[8];
											string operatename = content.body[9];
											string userno = content.body[10];
											DataTable t = DOWNTIME_TABLE.Updatedowntime(downtimeno, stoptype, machineno, starttime, endtime, serviceteam, stopreson, status, downcommom, operatename, userno);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.updatedowntime);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.updatedowntime", ex);
										}
										break;
									}
								case Identity.selectV7_DOWNTIME:
									{
										try
										{
											string sql = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string downtimeTotalPages = "";
											DataTable t = DOWNTIME_TABLE.SelectV7_DOWNTIME(sql, minRow, maxRow, out downtimeTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + downtimeTotalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.selectV7_DOWNTIME);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.selectV7_DOWNTIME", ex);
										}
										break;
									}
								case Identity.adddowntime:
									{
										try
										{
											string predowntimeNo = content.body[0];
											DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(content.body[1]);
											string userno = content.body[2];

											string downtimeno = dataTable.Rows[0][DOWNTIME_TABLE.fd_downtimeNo].ToString();
											string stoptype = dataTable.Rows[0][DOWNTIME_TABLE.fd_stopType].ToString();
											string machineno = dataTable.Rows[0][DOWNTIME_TABLE.fd_machineNo].ToString();
											string starttime = dataTable.Rows[0][DOWNTIME_TABLE.fd_startTime].ToString();
											string endtime = dataTable.Rows[0][DOWNTIME_TABLE.fd_endTime].ToString();
											string serviceteam = dataTable.Rows[0][DOWNTIME_TABLE.fd_serviceTeam].ToString();
											string stopreson = dataTable.Rows[0][DOWNTIME_TABLE.fd_stopReason].ToString();
											string status = dataTable.Rows[0][DOWNTIME_TABLE.fd_status].ToString();
											string downcommom = dataTable.Rows[0][DOWNTIME_TABLE.fd_downComment].ToString();
											string operatename = dataTable.Rows[0][DOWNTIME_TABLE.fd_operateName].ToString();

											DataTable t = DOWNTIME_TABLE.Adddowntime(predowntimeNo, downtimeno, stoptype, machineno, starttime, endtime, serviceteam, stopreson, status, downcommom, operatename, userno);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.updatedowntime);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.adddowntime", ex);
										}
										break;
									}

								#endregion

								#region Query Batch 
								case Identity.queryBatchByBatchNo:
									{
										try
										{
											string batchNo = content.body[0];							
											string stripTotalPages = "";
											string pipeTotalPages = "";
											Dictionary<string, double> dic;
											DataTable [] t = PIPE_INFO.QueryBatchByBatchNo(batchNo, out stripTotalPages,out pipeTotalPages,out dic);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + stripTotalPages + ";" + pipeTotalPages + ";" + JsonHelper.SerializeObject(dic));
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryTaskByTaskNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryTaskByTaskNo", ex);
										}
										break;
									}
								case Identity.queryBatchStripByBatchNo:
									{
										try
										{
											string batchNo = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string TotalPages = "";
											DataTable t = PIPE_INFO.QueryBatchStripByBatchNo(batchNo, minRow, maxRow, out TotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + TotalPages);
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryBatchStripByBatchNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryBatchStripByBatchNo", ex);
										}
										break;
									}
								case Identity.queryBatchPipeByBatchNo:
									{
										try
										{
											string batchNo = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string TotalPages = "";
											DataTable t = PIPE_INFO.QueryBatchPipeByBatchNo(batchNo, minRow, maxRow, out TotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + TotalPages);
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryBatchPipeByBatchNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryBatchPipeByBatchNo", ex);
										}
										break;
									}


								//case Identity.querySTRIP_INFOByTaskNo:
								//	{
								//		try
								//		{
								//			string task_No = content.body[0];
								//			int minRow = Convert.ToInt32(content.body[1]);
								//			int maxRow = Convert.ToInt32(content.body[2]);
								//			string totalPages = "";
								//			DataTable t = STRIP_INFO.Select_STRIP_INFOByTaskNo(task_No, minRow, maxRow, out totalPages);
								//			if (t != null)
								//			{
								//				string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
								//				content.session.Send(back);
								//			}
								//			else
								//			{
								//				string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
								//				content.session.Send(back);
								//			}
								//		}
								//		catch (Exception ex)
								//		{
								//			LogHelper.WriteLog("select_STRIP_INFOByTaskNo", ex);
								//			string back = FormatTcpString(content.commandId, "-1", ex.Message);
								//			content.session.Send(back);
								//		}
								//		break;
								//	}
								#endregion
								#region Query Strip
								case Identity.queryStripStripByStripNo:
									{
										try
										{
											string stripNo = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string totalPages = "";
											DataTable t = STRIP_INFO.QueryStripStripByStripNo(stripNo, minRow, maxRow, out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryStripStripByStripNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryStripStripByStripNo", ex);
										}
										break;
									}
								case Identity.queryStripPipeByStripNo:
									{
										try
										{
											string stripNo = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string totalPages = "";
											DataTable t = PIPE_INFO.QueryStripPipeByStripNo(stripNo, minRow, maxRow, out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryStripPipeByStripNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryStripPipeByStripNo", ex);
										}
										break;
									}
								#endregion
								#region Query Pipe 
								case Identity.queryPipePipeByPipeNo:
									{
										try
										{
											string pipeNo = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string totalPages = "";
											DataTable t = PIPE_INFO.QueryPipePipeByPipeNo(pipeNo, minRow, maxRow, out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryStripPipeByStripNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryStripPipeByStripNo", ex);
										}
										break;
									}
								case Identity.queryPipePipePassportAndTaskByPipeNo:
									{
										try
										{
											string pipeNo = content.body[0];
											Dictionary<string, string> dic;
											DataTable t = PIPE_INFO.QueryPipePipePassportAndTaskByPipeNo(pipeNo,out dic);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + JsonHelper.SerializeObject(dic));
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.QueryPipePipePassportAndTaskByPipeNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.QueryPipePipePassportAndTaskByPipeNo", ex);
										}
										break;
									}
								case Identity.queryPipeExportPipeProcessData:
									{
										try
										{
											string pipeNo = content.body[0];
											DataTable[] t = PIPE_INFO.QueryPipeExportPipeProcessData(pipeNo);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t));
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryPipeExportPipeProcessData");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "queryPipeExportPipeProcessData Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryPipeExportPipeProcessData", ex);
										}
										break;
									}
								case Identity.queryPIPE_INFOByStripNo:
									{
										try
										{
											string strip_No = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string totalPages = "";
											DataTable t = PIPE_INFO.QueryPIPE_INFOByStripNo(strip_No, minRow, maxRow, out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											LogHelper.WriteLog("select_STRIP_INFOByTaskNo", ex);
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
										}
										break;
									}
								case Identity.query_PIPE_PARAMETER:
									{
										try
										{
											string sql = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string queryPipeTotalPages = "";
											DataTable t = PIPE_INFO.Query_PIPE_PARAMETER(sql, minRow, maxRow, out queryPipeTotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + queryPipeTotalPages);
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryTaskByTaskNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryTaskByTaskNo", ex);
										}
										break;
									}
								#endregion
								#region Query Bundle  
								case Identity.queryBundleByBundleNo:
									{
										try
										{
											string bundleNo = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string totalPages = "";
											DataTable t = BUNDLE_TABLE.QueryBundleByBundleNo(bundleNo, minRow, maxRow, out totalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + totalPages);
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryBundlePipeByBundleNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "queryBundlePipeByBundleNo Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryBundlePipeByBundleNo", ex);
										}
										break;
									}
								case Identity.queryBundlePipeByBundleNo:
									{
										try
										{
											string bundleNo = content.body[0];
											Dictionary<string,string> dic;
											DataTable dicBundleTotal;
											Dictionary<string, DataTable> t = BUNDLE_TABLE.QueryBundlePipeByBundleNo(bundleNo, out dic,out dicBundleTotal);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + JsonHelper.SerializeObject(dic)+";"+ JsonHelper.SerializeObject(dicBundleTotal));
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryBundlePipeByBundleNo");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", "queryBundlePipeByBundleNo Error");
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryBundlePipeByBundleNo", ex);
										}
										break;
									}
								#endregion
								#region Query RecipeLog 
								case Identity.queryRecipeLog:
									{
										try
										{
											string sql = content.body[0];
											int minRow = Convert.ToInt32(content.body[1]);
											int maxRow = Convert.ToInt32(content.body[2]);
											string TotalPages = "";
											DataTable t = RECIPE_EVENT.QueryRecipeLog(sql, minRow, maxRow, out TotalPages);
											if (t != null)
											{
												string back = FormatTcpString(content.commandId, "1", JsonHelper.SerializeObject(t) + ";" + TotalPages);
												content.session.Send(back);
												LogHelper.WriteLog("HandleMessage.queryRecipeLog");
											}
											else
											{
												string back = FormatTcpString(content.commandId, "-1", ErrorInfo.select_STRIP_INFOByOrder_ID);
												content.session.Send(back);
											}
										}
										catch (Exception ex)
										{
											string back = FormatTcpString(content.commandId, "-1", ex.Message);
											content.session.Send(back);
											LogHelper.WriteLog("HandleMessage.queryRecipeLog", ex);
										}
										break;
									}
								#endregion

								default:
									{
										break;
									}
							}
						}
						else
						{
							//??
						}
					}
					await Task.Delay(10);
				}
			}
			catch (Exception ex)
			{
				LogHelper.WriteLog("HandleMessage", ex);
			}            
        }


        public void Add(CommandContent cmd)
        {
            messageList.Enqueue(cmd);
        }


        public bool Remove()
        {
            return false;
        }

        public void Stop()
        {
            IsHandling = false;
        }

        private string FormatTcpString(string id, string result, string content)
        {
            //send back: identity;time;result;content##
            string format = string.Format("{0};{1};{2};{3}##", id, DateTime.Now.ToString(Formats.GetDateFormat()), result, content);
            return format;
        }

    }
}
